package com.colis.quartz.utils;

import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import static com.colis.quartz.common.constant.JavaFileConstant.*;

/**
 * @description: 重写类加载器达到加载指定目录的类
 * @author: wlp
 * @createDate: 2020-01-06
 * @version: 1.0
 */
@Slf4j
public class MyClassLoader extends ClassLoader {

    @Override
    protected Class<?> findClass(String className) {
        String myPath = new File(BASE_DIR + className + SUFFIX).toURI().toString();
        log.info("自定义时class文件==》》" + myPath);
        Class<?> clazz = null;
        try {
            Path path = Paths.get(new URI(myPath));
            byte[] cLassBytes = Files.readAllBytes(path);
            clazz = defineClass(className, cLassBytes, 0, cLassBytes.length);
        } catch (IOException | URISyntaxException e) {
            e.printStackTrace();
            log.info("字节码文件读取出错");
        }
        return clazz;
    }
}
